package com.shoppingmall.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.android.material.tabs.TabLayout;
import com.shoppingmall.R;
import com.shoppingmall.bean.CollectGoodsBean;
import com.shoppingmall.fragment.DealerSubordinateDealersDetailsFragment;
import com.shoppingmall.fragment.MineCollectGoodsFragment;
import com.shoppingmall.fragment.MineCollectStoresFragment;


import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 2020/3/15
 * 描述：我的收藏
 */
public class MineCollectActivity extends BaseActivity {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.rl_cancel_collect)
    RelativeLayout rlCancelCollect;
    private List<BaseFragment> fragmentList;
    private String[] title = {"商品", "店铺"};

    private boolean isModifyState;//是否是编辑状态

    private int selectPos = 0;

    @Override
    protected int initLayout() {
        return R.layout.activity_mine_collect;
    }

    @Override
    protected void initView() {
        selectPos = getIntent().getIntExtra("selectPos", 0);
        fragmentList = new ArrayList<>();
        fragmentList.add(new MineCollectGoodsFragment());

        fragmentList.add(new DealerSubordinateDealersDetailsFragment());

        fragmentList.add(new MineCollectStoresFragment());

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(viewPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(selectPos).select();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isModifyState) {
                    titleBar.setRightText("编辑");
                    rlCancelCollect.setVisibility(View.GONE);
                    isModifyState = false;

                } else {
                    titleBar.setRightText("完成");
                    rlCancelCollect.setVisibility(View.VISIBLE);
                    isModifyState = true;
                }
                if (null != onModifyListener)
                    onModifyListener.modify(isModifyState);

            }
        });
        rlCancelCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != onModifyListener)
                    onModifyListener.cancel();
            }
        });
    }

    private class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return title[position];
        }
    }

    private OnModifyListener onModifyListener;

    public void setOnModifyListener(OnModifyListener onModifyListener) {
        this.onModifyListener = onModifyListener;
    }

    public interface OnModifyListener {

        void modify(boolean isModifyState);

        void cancel();
    }


}
